package com.tfswx.jcyw.dmzc.window;

import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.tfswx.jcyw.dmzc.api.ApiUtil;
import com.tfswx.jcyw.dmzc.api.dto.DmzcGetOutputDTO;
import com.tfswx.jcyw.dmzc.api.dto.DownloadInputDTO;
import com.tfswx.jcyw.dmzc.service.DlxxService;
import com.tfswx.jcyw.dmzc.util.MessageUtil;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author ricky
 * @date 2022/10/18
 */
public class DmzcjlViewWindow extends DialogWrapper {
    private final DmzcGetOutputDTO dmzcGetOutputDTO;
    private final DlxxService dlxx = DlxxService.getInstance().getState();
    private final byte[] tpBytes;
    private JPanel windowPanel;
    private JLabel zcwjLabel;
    private JLabel kfryLabel;
    private JLabel zcyjLabel;
    private JLabel zcbzflLabel;
    private JLabel tpLabel;
    private JLabel zcxmLabel;
    private JLabel zcbzLabel;
    private JLabel zcryLabel;
    private JScrollPane mainScrollPane;
    private JLabel yjdjLabel;

    protected DmzcjlViewWindow(DmzcGetOutputDTO dmzcGetOutputDTO) {
        super(false);
        init();

        this.dmzcGetOutputDTO = dmzcGetOutputDTO;
        setTitle("查看记录详情");

        mainScrollPane.setBorder(BorderFactory.createEmptyBorder());
        mainScrollPane.getVerticalScrollBar().setUnitIncrement(16);

        // 获取图片
        DownloadInputDTO downloadInputDTO = new DownloadInputDTO();
        downloadInputDTO.setWjbh(dmzcGetOutputDTO.getJtwjbh());
        tpBytes = ApiUtil.fwdApi.download(downloadInputDTO);

        initui();
        tpLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
                    // 左键双击图片
                    new TpViewDialog(tpBytes).show();
                }
            }
        });
    }

    private void initui() {
        try {
            // 图片
            ImageIcon tp = new ImageIcon(tpBytes);

            int iconWidth = tp.getIconWidth();
            int iconHeight = tp.getIconHeight();
            Image image = tp.getImage();
            int maxSize = Math.max(iconWidth, iconHeight);
            int scale = (int) Math.ceil(maxSize / 500.0);
            int newHeight = iconHeight / scale;
            int newWidth = iconWidth / scale;

            image = image.getScaledInstance(newWidth, newHeight, Image.SCALE_DEFAULT);
            tp.setImage(image);
            tpLabel.setIcon(tp);

            zcwjLabel.setText(dmzcGetOutputDTO.getZcywjbh());
            kfryLabel.setText(dmzcGetOutputDTO.getKfrymc());
            zcyjLabel.setText(dmzcGetOutputDTO.getZcyj());
            zcbzflLabel.setText(dmzcGetOutputDTO.getPsbzflmc());
            zcbzLabel.setText(dmzcGetOutputDTO.getJcnr());
            zcxmLabel.setText(dmzcGetOutputDTO.getGcmc());
            zcryLabel.setText(dmzcGetOutputDTO.getZcrymc());
            yjdjLabel.setText(dmzcGetOutputDTO.getYjdj());

        } catch (Exception e) {
            MessageUtil.showErrorDialog(e.getMessage(), "页面初始化失败，请检查！");
            close(CANCEL_EXIT_CODE);
        }
    }

    @Override
    protected JComponent createCenterPanel() {
        return windowPanel;
    }

    @Override
    protected Action @NotNull [] createActions() {
        Action okAction = new AbstractAction("关闭") {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                close(OK_EXIT_CODE);
            }
        };
        return new Action[]{okAction};
    }
}
